﻿@page "/sys/menu"
@using System.Linq.Expressions;
@using BlazorComponent;
@using Masa.Blazor.Presets;
@inject IMenuService MenuService
@namespace ThingsBlazor.Web.Rcl
@attribute [Authorize]
@inject MasaBlazor MasaBlazor
@attribute [RolePermission]
@inherits CultureComponentBase
@inject UserResoures UserResoures
@layout MainLayout
<MRow>
    <MCol Md=3 Cols="12">
        <MCard Class="ma-2" Style="height:100%">
            <MCardTitle>
                <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="mx-2 my-1" @bind-Value="_searchName"
                            Outlined Label=@typeof(SysResource).GetDescription(nameof(SysResource.Title)) />
            </MCardTitle>
            <MTreeview Dense TItem="SysResource" TKey="long" ActiveChanged=@(async a=>
                       { search.ParentId=a.FirstOrDefault();await datatableQuery(); } )
                       Items="MenuCatalog" ItemText="r=>r.Title" ItemChildren="r=>r.Children"
                       Search="@_searchName"
                       Activatable ItemKey=@(r=>r.Id)>
            </MTreeview>
        </MCard>
    </MCol>
    <MCol Md=9 Cols="12">
        <div style="height: calc(100vh - 200px)">

            <AppDataTable @ref="_datatable" TItem="SysResource" SearchItem="MenuPageInput"
                          AddItem="MenuAddInput" EditItem="MenuEditInput"
                          SearchModel="@search" IsMenuOperTemplate=false
                          QueryCall="QueryCall" AddCall="AddCall" EditCall="EditCall" DeleteCall="DeleteCall"
                          FilterHeaders="FilterHeaders" Filters="Filters" ShowDetailButton
                          ShowQueryButton IsShowSearchKey
                          ShowAddButton=@UserResoures.IsHasButtonWithRole("sysspaadd")
                          ShowDeleteButton=@UserResoures.IsHasButtonWithRole("sysspadelete")
                          ShowEditButton=@UserResoures.IsHasButtonWithRole("sysspaedit")>

                <AddTemplate>
                    @if (search.ParentId == 0)
                    {
                        context.TargetType = TargetTypeEnum.CATALOG;
                    }
                    <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Title))  </MSubheader>
                    <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Title />

                    <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Component))  </MSubheader>
                    <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Component />

                    <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.TargetType))  </MSubheader>
                    <MSelect 
                             Class="mt-3 mr-3" @bind-Value="context.TargetType" Outlined
                             Items=@(typeof(TargetTypeEnum).GetEnumList()) Clearable
                             MenuProps="@(props => { props.Bottom = true; props.OffsetY = true; })"
                             ItemText=@((u) =>u.des)
                             ItemValue=@(u =>(TargetTypeEnum)u.value)
                             HideDetails="true" Height="30"
                             Dense>
                    </MSelect>


                    <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Name))  </MSubheader>
                    <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Name />

                    <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Icon))  </MSubheader>
                    <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Icon />

                    <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
                    <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int  ThumbLabel="@("always")" Dense />




                </AddTemplate>

                <EditTemplate>
                    <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Title))  </MSubheader>
                    <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Title />

                    <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Component))  </MSubheader>
                    <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Component />

                    <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.TargetType))  </MSubheader>

                    <MSelect 
                             Class="mt-3 mr-3" @bind-Value="context.TargetType" Outlined
                             Items=@(typeof(TargetTypeEnum).GetEnumList()) Clearable
                             MenuProps="@(props => { props.Bottom = true; props.OffsetY = true; })"
                             ItemText=@((u) =>u.des)
                             ItemValue=@(u =>(TargetTypeEnum)u.value)
                             HideDetails="true" Height="30"
                             Dense>
                    </MSelect>
                    <MSubheader Class="mt-4 font-weight-black">@T("改变父级")</MSubheader>
                    <MSelect Class="mr-3" @bind-Value=@context.ParentId Outlined
                             Items=@(MenuCatalog) 
                             MenuProps="@(props => { props.Bottom = true; props.OffsetY = true; })"
                             ItemText=@((u) =>u.Title) ItemValue=@(u =>u.Id)
                             HideDetails=@("auto") Height="30" Dense>
                    </MSelect>


                    <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Name))  </MSubheader>
                    <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Name />

                    <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Icon))  </MSubheader>
                    <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Icon />

                    <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
                    <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int ThumbLabel="@("always")" Dense />


                </EditTemplate>

                <ItemColOperTemplate>
                    @if (context.Item.TargetType == TargetTypeEnum.SELF || context.Item.TargetType == TargetTypeEnum.BLANK)
                    {
                        <MButton Color="info" Text StopPropagation OnClick=@(()=>showButtonList(context.Item.Id))>
                            @T("按钮")
                        </MButton>
                    }
                </ItemColOperTemplate>

                <ItemColTemplate>
                    @switch (context.Header.Value)
                    {
                        case nameof(context.Item.Icon):
                            <MIcon>
                                @context.Value
                            </MIcon>
                            break;
                        default:
                            @context.Value
                            break;
                    }

                </ItemColTemplate>
            </AppDataTable>
        </div>
    </MCol>
</MRow>

<PDrawer @bind-Value="IsShowButtonList" OnCancel="() => IsShowButtonList = false"
         Title=@T("按钮")
         Width=@(MasaBlazor.Breakpoint.Mobile?"100%":"800")
         MaxWidth="800">
    @if (IsShowButtonList)
    {
        <AppDataTable @ref="_buttonsDatatable" TItem="SysResource" SearchItem="ButtonPageInput"
                  AddItem="ButtonAddInput" EditItem="ButtonEditInput"
                  QueryCall="ButtonQueryCall" AddCall="ButtonAddCall" EditCall="ButtonEditCall" DeleteCall="ButtonDeleteCall"
                  FilterHeaders="ButtonFilterHeaders"
                  ShowQueryButton  IsShowClearSearch=false
                  ShowAddButton=@UserResoures.IsHasButtonWithRole("sysspaadd")
                  ShowDeleteButton=@UserResoures.IsHasButtonWithRole("sysspadelete")
                  ShowEditButton=@UserResoures.IsHasButtonWithRole("sysspaedit")>

            <AddTemplate>
                <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Title))  </MSubheader>
                <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Title />

                <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Code))  </MSubheader>
                <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Code />


                <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
                <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int ThumbLabel="@("always")" Dense />



            </AddTemplate>

            <EditTemplate>
                <MSubheader Class="mt-4 font-weight-black"> @(context.Description(x => x.Title))  </MSubheader>
                <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Title />

                <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Code))  </MSubheader>
                <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Code />


                <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
                <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int ThumbLabel="@("always")" Dense />


            </EditTemplate>

        </AppDataTable>

    }

</PDrawer>



@code {
    string _searchName;
    long buttonParentId;
    bool IsShowButtonList;
    IAppDataTable _buttonsDatatable;
    [Inject]
    IButtonService ButtonService { get; set; }
    private async Task ButtonAddCall(ButtonAddInput input)
    {
        input.ParentId = buttonParentId;
        await ButtonService.Add(input);
    }
    private async Task ButtonDeleteCall(IEnumerable<SysResource> input)
    {
        await ButtonService.Delete(input.ToList().ConvertAll(it => new BaseIdInput()
            { Id = it.Id }));
    }

    private async Task ButtonEditCall(ButtonEditInput input)
    {
        await ButtonService.Edit(input);

    }
    private async Task<SqlSugarPagedList<SysResource>> ButtonQueryCall(ButtonPageInput input)
    {
        input.ParentId = buttonParentId;
        var data = await ButtonService.Page(input);
        return data;
    }
    private async Task showButtonList(long parentId)
    {
        buttonParentId = parentId;
        IsShowButtonList = true;
        if (_buttonsDatatable != null)
            await _buttonsDatatable.QueryClick();
    }

    private void ButtonFilterHeaders(List<DataTableHeader<SysResource>> datas)
    {
        datas.RemoveWhere(it => it.Value == nameof(SysResource.Category));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.CreateTime));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.UpdateTime));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.CreateUser));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.TargetType));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.UpdateUser));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.ParentId));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.CreateUserId));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.UpdateUserId));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.Icon));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.Name));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.Component));

        datas.RemoveWhere(it => it.Value == nameof(SysResource.IsDelete));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.ExtJson));
        datas.RemoveWhere(it => it.Value == nameof(SysResource.Id));

        datas.RemoveWhere(it => it.Value == nameof(SysResource.Children));

        foreach (var item in datas)
        {
            item.Sortable = false;
            item.Filterable = false;
            item.Divider = false;
            item.Align = DataTableHeaderAlign.Start;
            item.CellClass = " table-minwidth ";
            switch (item.Value)
            {
                case nameof(SysResource.Title):
                    item.Sortable = true;
                    break;

                case nameof(SysResource.SortCode):
                    item.Sortable = true;
                    break;
            }
        }
    }

}
